import {defineStore} from "pinia"

export const useCart = defineStore('carts',{
    state(){
        return{
            cart:[],
        }
    },
    getters: {
        total(state) {
            return (state.cart.reduce((total, item) => {
                return total += item.price * item.count
            }, 0)).toFixed(2)
        }
    },
    actions:{
        add_product(it){
            let target = this.cart.find(item => item.id === it.id)
            if (!target) {
                let obj = {
                    title: it.title,
                    price: it.price,
                    count: 1,
                    id: it.id
                }
                this.cart.push(obj)
            } else {
                target.count++
            }
        },
        // del_product(it){
        //     it.
        // }
    }
})